#!/bin/bash

#-------------------------------------------------------------------------------
# Installation return codes:
#
# 0 - no error
# 1 - usage, input argument incorrect
# 2 - unable to mount service media
# 3 - error unzip'ing file on disk
# 4 - .signature file missing from service package
# 5 - service install script missing
# 6 - incompatible service release
# 7 - incorrect file successfully downloaded to system - better not happen!
# 8* - error executing install script, return the script error instead
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
# Initialize service script return code
#-------------------------------------------------------------------------------
serviceRC=0

#-------------------------------------------------------------------------------
# Common exit point
#-------------------------------------------------------------------------------
exit_cleanup() {
   if [ $1 -eq 0 ]
   then
      exit 0
   else
      if [ $1 -lt 8 ]
      then
         exit $1
      else
         exit $serviceRC
      fi
   fi
}

if [ "$*" = "" ]
then
   echo "Usage: applyUpdates [cdrom | disk]"
   exit_cleanup 1
fi
cv=`/opt/hsc/bin/hsc version | grep Release | cut -d':' -f 2`
case $1 in

cdrom )
   mount /mnt/cdrom
   if [ $? -ne 0 ]
   then
     echo "Unable to mount media" 1>&2
     exit_cleanup 2
   fi
   if [ ! -f /mnt/cdrom/.signature ]
   then
     echo "Signature file missing from media" 1>&2
     umount /mnt/cdrom
     exit_cleanup 4
   else
     x=`cat /mnt/cdrom/.signature`
     fv=`echo $x | cut -d':' -f 1`
     fr=`echo $x | cut -d':' -f 2`
     if [ "$fv" = "HSC Update Software" -a "$fr" = "$cv" ]
     then
        if [ ! -x /mnt/cdrom/applyHSCUpdates ]
        then
           echo "Media install script not found" 1>&2
           umount /mnt/cdrom
           exit_cleanup 5
        else
           /mnt/cdrom/applyHSCUpdates
           serviceRC=$?
           if [ $serviceRC -ne 0 ]
           then
              exit_cleanup 6
           fi
        fi
     else
        echo "Incompatible service media inserted" 1>&2
        umount /mnt/cdrom
        exit_cleanup 6
     fi
   fi
   
   umount /mnt/cdrom
   ;;
   
disk )
   if [ -f /usr/local/hsc_install.images/*.zip ]
   then
      /usr/bin/unzip -o /usr/local/hsc_install.images/*.zip -d /usr/local/hsc_install.images
      if [ $? -ne 0 ]
      then
         exit_cleanup 3
      fi
      
      if [ ! -f /usr/local/hsc_install.images/.signature ]
      then
         echo "Signature file missing from downloaded package" 1>&2
         rm -rf /usr/local/hsc_install.images/*
         rm -rf /usr/local/hsc_install.images/.signature
         exit_cleanup 4
      else
         x=`cat /usr/local/hsc_install.images/.signature`
         fv=`echo $x | cut -d':' -f 1`
         fr=`echo $x | cut -d':' -f 2`
         if [ "$fv" = "HSC Update Software" -a "$fr" = "$cv" ]
         then
            if [ ! -x /usr/local/hsc_install.images/applyHSCUpdates ]
            then
              echo "Media install script not found" 1>&2
              rm -rf /usr/local/hsc_install.images/*
              rm -rf /usr/local/hsc_install.images/.signature
              exit_cleanup 5
            else
              /usr/local/hsc_install.images/applyHSCUpdates
              serviceRC=$?
              if [ $serviceRC -eq 0 ]
              then
                 rm -rf /usr/local/hsc_install.images/*
                 rm -rf /usr/local/hsc_install.images/.signature
              else
                 exit_cleanup 8
              fi
            fi
         else
            echo "Incompatible service package" 1>&2
            rm -rf /usr/local/hsc_install.images/*
            rm -rf /usr/local/hsc_install.images/.signature
            exit_cleanup 6
         fi
      fi
   else
      exit_cleanup 7
   fi
   ;;
esac

exit_cleanup 0

